package com.lan.service.wealth;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lan.entity.wealth.Wealth;
import com.lan.mapper.wealth.WealthMapper;
import com.lan.viewObject.wealth.WealthObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 财富报表 服务实现类
 * </p>
 *
 * @author shadow
 * @since 2022-09-03
 */
@Service
public class WealthServiceImpl extends ServiceImpl<WealthMapper, Wealth> implements WealthService {

    @Resource
    private WealthMapper wealthMapper;

    @Override
    public WealthObject getWealthObject() {

        List<String> dateList = new ArrayList<>();
        dateList.add("2022-09-05 23:59:59");
        dateList.add("2022-09-06 23:59:59");
        dateList.add("2022-09-07 23:59:59");
        dateList.add("2022-09-08 23:59:59");
        dateList.add("2022-09-09 23:59:59");
        dateList.add("2022-09-10 23:59:59");
        dateList.add("2022-09-11 23:59:59");
        dateList.add("2022-09-12 23:59:59");

        List<BigDecimal> totalMoneyList = getTotalMoneyList(dateList);
        WealthObject wealth = new WealthObject();
        BigDecimal totalMoney = getTotalMoney();
        BigDecimal totalDebt = getTotalDebt();
        BigDecimal realMoney = totalMoney.subtract(totalDebt);
        BigDecimal debtPercentage = totalDebt.divide(totalMoney, 6, BigDecimal.ROUND_HALF_UP)
                .multiply(new BigDecimal("100"))
                .setScale(4, BigDecimal.ROUND_HALF_UP);
        List<Wealth> moneyList = getMoneyList();
//        List<String> resourceList = moneyList.stream().map(Wealth::getResource).collect(Collectors.toList());
        for (int i = 0; i < dateList.size();i++) {
            dateList.set(i, dateList.get(i).substring(5, 10));
        }
        wealth.setTotalMoney(totalMoney)
                .setTotalDebt(totalDebt)
                .setRealMoney(realMoney)
                .setDebtPercentage(debtPercentage)
                .setTotalMoneyList(totalMoneyList)
                .setDateList(dateList)
                .setChangeList(moneyList);
        return wealth;
    }

    /**
     * 获取最新的总资金
     *
     */
    @Override
    public BigDecimal getTotalMoney() {
        return wealthMapper.getTotalMoney(1);
    }

    /**
     * 获取最新的总负债
     *
     */
    @Override
    public BigDecimal getTotalDebt() {
        return wealthMapper.getTotalMoney(2);
    }

    /**
     * 获取最新的资金 list
     */
    @Override
    public List<Wealth> getMoneyList() {
        return wealthMapper.getMoneyList(1);
    }

    /**
     * 获取最新的负债 list
     */
    @Override
    public List<Wealth> getDebtList() {
        return wealthMapper.getMoneyList(2);
    }

    /**
     * 总资金 list
     */
    @Override
    public List<BigDecimal> getTotalMoneyList(List<String> dateList) {
        List<BigDecimal> list = new ArrayList<>();
        for (String i : dateList) {
            BigDecimal a = wealthMapper.getTotalMoneyList(1,i);
            list.add(a);
        }
        return list;
    }

    /**
     * 总负债 list
     */
    @Override
    public List<Wealth> getTotalDebtList() {
        return null;
    }

}
